package xml

import (
	"encoding/xml"
	"errors"
)

type Person struct {
	Name    string `xml:"name"`
	Age     int    `xml:"age"`
	Address string `xml:"address"` // 该属性转 XML 时会被忽略
}

func ToXMLString(data interface{}) (string, error) {
	xmlStr, err := xml.Marshal(data)
	if err != nil {
		return "", err
	}
	return string(xmlStr), nil
}

func GetObject(xmlStr string) (*Person, error) {
	var p *Person
	if len(xmlStr) == 0 {
		return nil, errors.New("xml string is nil")
	}
	err := xml.Unmarshal([]byte(xmlStr), &p)
	if err != nil {
		return nil, err
	}
	return p, nil
}
